package com.zprmb.udzt;

import java.text.DecimalFormat;

import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

/**
 * 我的银行卡
 * 
 * @author shenshao
 * 
 */
public class MyBankCard extends BaseActivity implements OnClickListener {
	private RelativeLayout rl_stand_bank, rl_bank, rl_xiane;
	private ImageView iv_v4_addmoney_success_bankioc, img_back;;
	private TextView tv_please, bank_num, xiane, bank_name;
	private String bankNo = "0";
	private boolean isbind;
	private RelativeLayout rl_back;
	private LinearLayout li_all;
	private LinearLayout li_bankinfo;
	private TextView tv_customer;
	private DecimalFormat df3 = new DecimalFormat("###");
	private SharedPreferences sha;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_bankcard);
		BbtApplication.getInstance().addActvity(this);
		sha = getSharedPreferences("bank", MODE_PRIVATE);
		if (sha.getInt("SPDB", 9527) != 0) {
			Editor editor = sha.edit();
			editor.putInt("ICBC", R.drawable.bbt_icbc1);
			editor.putInt("ABC", R.drawable.bbt_abc1);
			editor.putInt("BOC", R.drawable.bbt_boc1);
			editor.putInt("CCB", R.drawable.bbt_ccb1);
			editor.putInt("CMB", R.drawable.bbt_cbm1);
			editor.putInt("CEB", R.drawable.bbt_ceb1);
			editor.putInt("SPDB", R.drawable.bbt_spdb1);
			editor.putInt("CIB", R.drawable.bbt_cib1);
			editor.putInt("PSBC", R.drawable.bbt_psbc1);
			editor.putInt("HXB", R.drawable.bbt_hxb1);
			editor.putInt("BCCB", R.drawable.bbt_bccb1);
			editor.putInt("BOS", R.drawable.bbt_bos1);
			editor.putInt("CGB", R.drawable.bbt_cgb1);
			editor.putInt("CMSB", R.drawable.bbt_cmsb1);
			editor.putInt("CNCB", R.drawable.bbt_cncb1);
			editor.putInt("COMM", R.drawable.bbt_comm1);
			editor.putInt("HZCB", R.drawable.bbt_hzcb1);
			editor.putInt("NBCB", R.drawable.bbt_nbcb1);
			editor.putInt("PAB", R.drawable.bbt_pab1);
			editor.putInt("BCCB", R.drawable.bbt_bccb1);
			editor.commit();
		}
		iv_v4_addmoney_success_bankioc = (ImageView) findViewById(R.id.iv_v4_addmoney_success_bankioc);
		img_back = (ImageView) findViewById(R.id.img_back);
		li_all = (LinearLayout) findViewById(R.id.li_all);
		tv_please = (TextView) findViewById(R.id.please);
		rl_bank = (RelativeLayout) findViewById(R.id.rl_bank);
		xiane = (TextView) findViewById(R.id.tv_xiane);
		li_bankinfo = (LinearLayout) findViewById(R.id.li_bankinfo);
		rl_xiane = (RelativeLayout) findViewById(R.id.rl_xiane);
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		tv_customer = (TextView) findViewById(R.id.enterprise_tel);
		bank_name = (TextView) findViewById(R.id.bank_name);
		tv_customer.setOnClickListener(this);
		li_all.setVisibility(View.GONE);
		rl_back.setOnClickListener(this);
		rl_bank.setOnClickListener(this);
		img_back.setOnClickListener(this);
		bank_num = (TextView) findViewById(R.id.bank_num);
		rl_stand_bank = (RelativeLayout) findViewById(R.id.rl_stand_bank);
		rl_stand_bank.setOnClickListener(this);

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("绑卡", jsonObject.toString());
				if (jsonObject.optInt("success") == 1) {
					li_all.setVisibility(View.VISIBLE);
					rl_bank.setEnabled(false);
					bank_num.setVisibility(View.VISIBLE);
					isbind = true;
					JSONObject json = jsonObject.optJSONObject("data");
					tv_please.setVisibility(View.GONE);
					bankNo = json.optString("bank");
					li_bankinfo.setVisibility(View.VISIBLE);
					bank_name.setText(json.optString("bankName"));
					rl_bank.setBackgroundColor(Color.parseColor(json
							.optString("color")));
					iv_v4_addmoney_success_bankioc.setImageResource(sha.getInt(
							json.optString("bank"), 0));
					bank_num.setText("**** **** **** "
							+ json.optString("cardNo"));
					rl_xiane.setVisibility(View.VISIBLE);
					xiane.setTextColor(Color.parseColor(json.optString("color")));
					xiane.setText(json.optString("tips"));
				} else {
					rl_bank.setEnabled(true);
					isbind = false;
					li_bankinfo.setVisibility(View.GONE);
					li_all.setVisibility(View.VISIBLE);
					rl_xiane.setVisibility(View.GONE);
					rl_bank.setBackgroundResource(R.drawable.jia_bank);
				}
			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub
				Toast.makeText(MyBankCard.this, "请求失败,请检查网络设置", 2000).show();
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(MyBankCard.this, Constant.URL + "/api/v5/user/self",
				"");
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.img_back:
			finish();
			break;
		case R.id.rl_stand_bank:
			Intent intent2 = new Intent(MyBankCard.this, Standby_bank.class);
			intent2.putExtra("bank", true);
			if (isbind) {
				intent2.putExtra("bankNo", bankNo);
			} else {
				intent2.putExtra("bankNo", "2");
			}
			startActivity(intent2);
			break;
		case R.id.rl_bank:
			Intent intent = new Intent(MyBankCard.this, BankCardActivity.class);
			intent.putExtra("who", 3);
			startActivity(intent);
			break;
		case R.id.rl_back:
			finish();
			break;
		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}
	}

}
